package com.v2future.sjt.collegecheckingattendance.personal;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.bean.LoginResultBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.MD5;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2016/2/22.
 * 修改密码
 */
public class ChangePwdActivity extends MainActivity implements View.OnClickListener{

    private EditText oldPwdEt,newPwdEt,comfirmPwdEt;

    private Button submitBtn;

    /**
     * init first
     */
    @Override
    public void init() {

    }

    /**
     * init view
     */
    @Override
    public void initView() {
        setContentView(R.layout.activity_change_pwd);
        initTitleBack(null);
        setTitleText(getString(R.string.change_pwd));
        oldPwdEt = (EditText) findViewById(R.id.et_o_pwd);
        newPwdEt = (EditText) findViewById(R.id.et_new_pwd);
        comfirmPwdEt = (EditText) findViewById(R.id.et_comfirm_pwd);
        submitBtn = (Button) findViewById(R.id.btn_submit);
        submitBtn.setOnClickListener(this);
    }

    /**
     * init after
     */
    @Override
    public void initAfter() {

    }

    public void doSubmitChange(final String oldPasswordMd5, final String newPasswordMd5, final String sourceTypeId){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.MODIFY_PWD));
        params.addQueryStringParameter("userPassword", oldPasswordMd5);
        params.addQueryStringParameter("newUserPassword", newPasswordMd5);
        params.addQueryStringParameter("sourceTypeId", sourceTypeId);
        showLoadingDialog();
        NetUtils.doGetRequest(this, params, new NetTaskListener<BaseResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(BaseResponseBean result) {
                showOneBtnDialog(getString(R.string.notice), "修改密码成功", getString(R.string.iknow), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ChangePwdActivity.this.finish();
                    }
                });
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(BaseResponseBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelLoadingDialog();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                //校验用户身份
                LoginResultBean userInfo = getUserInfo();
                if (null == userInfo) break;
                if (TextUtils.isEmpty(userInfo.getData().getSourceTypeId())) {
                    showSimpleDialog("您的身份有误");
                    break;
                }
                //检验密码格式
                String old = oldPwdEt.getText().toString();
                String newp = newPwdEt.getText().toString();
                String comfirm = comfirmPwdEt.getText().toString();
                if (!TextUtils.isEmpty(old) &&
                        !TextUtils.isEmpty(newp) &&
                        !TextUtils.isEmpty(comfirm) &&
                        newp.equals(comfirm)){
                    try {
                        doSubmitChange(MD5.getMD5(old), MD5.getMD5(newp), userInfo.getData().getSourceTypeId());
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    showSimpleDialog("你的输入有误");
                }
                break;
        }
    }
}
